package repository

import (
	"errors"

	"github.com/lab-online/internal/report/constant"
	"github.com/lab-online/internal/report/entity"
	"gorm.io/gorm"
)

type Repository struct {
	db *gorm.DB
}

var _ interface {
	SaveReport(entity.ReportEntity) (entity.ReportEntity, error)
	GetReportByID(reportID uint) (entity.ReportEntity, error)
	SearchReport(entity.ReportEntity) ([]entity.ReportEntity, error)
	GetReportByStudentAndClassIDAndExperimentID(
		student string, classID, experimentID uint,
	) (entity.ReportEntity, error)
} = &Repository{}

func New(postgres *gorm.DB) *Repository {
	return &Repository{postgres}
}

func (r *Repository) errorHandler(err error) error {
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return constant.DBErrReportNotFound
	}

	return constant.DBErrUnknown
}
